﻿#include "DialogTransferFunction.h"
#include "ui_DialogTransferFunction.h"

#include "AnimationSettingData.h"
#include "DataChosenListDialog.h"
#include <QIntValidator>
#include "IdaWidgetHelper.h"
#include "IdaDialogMsg.h"
#include <QListView>
#include <QDebug>

#include "BaseModule/IBaseSignal.h"

namespace pst
{
    DialogTransferFunction::DialogTransferFunction(QWidget* parent)
        : IdaDialogFrame(parent), ui(new Ui::DialogTransferFunction())
    {
        ui->setupUi(this);

        setWindowTitle(tr("传递函数"));
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮

        ui->pushButton_Curve->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_Curve->setStyleSheet("QPushButton { text-align: center; }");
        ui->pushButton_CalibrationCurve->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_CalibrationCurve->setStyleSheet("QPushButton { text-align: center; }");

        ui->comboBox_TransferFunctionType->setView(new QListView());
    }

    DialogTransferFunction::~DialogTransferFunction()
    {
        delete ui;
    }

    void DialogTransferFunction::setCurveNameList(const QList<QString>& curveNameList)
    {
        m_curveNameList = curveNameList;
        m_model.setStringList(m_curveNameList);
    }

    void DialogTransferFunction::setInit(QStringList& params)
    {
        if (params.at(0) != "3")
            return;
        params.removeFirst();
        QString funcType = params.back();
        if (funcType == "电流")
        {
            ui->comboBox_TransferFunctionType->setCurrentIndex(0);
        }
        else if (funcType == "电场")
        {
            ui->comboBox_TransferFunctionType->setCurrentIndex(1);
        }

        params.removeLast();

        slot_sentChosenDataIds(createSelectedIndexes(QStringList() << params.front()));
        slot_sentCalibrationChosenDataIds(createSelectedIndexes(QStringList() << params.back()));
    }

    void DialogTransferFunction::on_pushButton_confirm_clicked()
    {
        if (!m_canConfirm)
        {
            auto _text = QString("存在未选择曲线情况，请选择曲线！");
            auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
            return;
        }
        QString currentSelected, calibrationSelected, type;
        if (m_transferFunctionType == 0 && m_CurrentSelectedIndexs.size() == 1)
        {
            type = "电流";
            currentSelected = m_curveNameList.at(m_CurrentSelectedIndexs.first());
        }
        else if (m_transferFunctionType == 1 && m_FieldStrengthSelectedIndexs.size() == 1)
        {
            type = "电场";
            currentSelected = m_curveNameList.at(m_FieldStrengthSelectedIndexs.first());
        }
        if (m_CalibrationSelectedIndexs.size() == 1)
        {
            calibrationSelected = m_curveNameList.at(m_CalibrationSelectedIndexs.first());
        }

        QString _pyCode = QString("PostProcessing.lineChartDialogTransferFunction('%1','%2','%3')").arg(currentSelected).arg(calibrationSelected).arg(type);
        emit IBaseSignalInstance->signal_execPYCode(QStringList{_pyCode}, true, false);
        emit signal_sendSelectInfo(currentSelected, calibrationSelected, type);
        IdaDialogFrame::accept();
    }

    void DialogTransferFunction::on_pushButton_cancel_clicked()
    {
        this->close();
    }

    void DialogTransferFunction::pushButtonConfirmStateUpdata()
    {
        if (m_transferFunctionType == 0)
        {
            if (!m_CurrentSelectedIndexs.isEmpty())
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, true, Qt::blue);
            else
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, false, Qt::white);
        }
        else if (m_transferFunctionType == 1)
        {
            if (!m_FieldStrengthSelectedIndexs.isEmpty())
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, true, Qt::blue);
            else
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, false, Qt::white);
        }

        if (!m_CalibrationSelectedIndexs.isEmpty())
            IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_CalibrationCurve, true, Qt::blue);
        else
            IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_CalibrationCurve, false, Qt::white);

        // 需求更改，这部分功能改为报错弹窗
        // 验证是否可以确定
        if (m_transferFunctionType == 0)
        {
            if (m_CurrentSelectedIndexs.count() > 0 && m_CalibrationSelectedIndexs.count() > 0)
                m_canConfirm = true;
            else
                m_canConfirm = false;
        }
        else if (m_transferFunctionType == 1)
        {
            if (m_FieldStrengthSelectedIndexs.count() > 0 && m_CalibrationSelectedIndexs.count() > 0)
                m_canConfirm = true;
            else
                m_canConfirm = false;
        }
        // ui->pushButton_confirm->setEnabled(_canConfirm);
    }

    void DialogTransferFunction::slot_sentChosenDataIds(const QModelIndexList& chosenID)
    {
        if (m_transferFunctionType == 0)
        {
            m_CurrentSelectedIndexs.clear();
        }
        else if (m_transferFunctionType == 1)
        {
            m_FieldStrengthSelectedIndexs.clear();
        }
        auto count = chosenID.count();
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();

                auto index = chosenID.at(i).row();

                if (m_transferFunctionType == 0)
                {
                    m_CurrentSelectedIndexs.append(index);
                }
                else if (m_transferFunctionType == 1)
                {
                    m_FieldStrengthSelectedIndexs.append(index);
                }
            }
        }
        pushButtonConfirmStateUpdata();
    }

    void DialogTransferFunction::slot_sentCalibrationChosenDataIds(const QModelIndexList& chosenID)
    {
        m_CalibrationSelectedIndexs.clear();
        auto count = chosenID.count();
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();
                auto index = chosenID.at(i).row();
                m_CalibrationSelectedIndexs.append(index);
            }
        }
        pushButtonConfirmStateUpdata();
    }

    void DialogTransferFunction::on_comboBox_TransferFunctionType_currentIndexChanged(int index)
    {
        m_transferFunctionType = index;
        if (index == 0)
        {
            ui->label_Curve->setText(QStringLiteral("选择感应电流曲线"));
        }
        else if (index == 1)
        {
            ui->label_Curve->setText(QStringLiteral("选择场强曲线"));
        }
        pushButtonConfirmStateUpdata();
    }
    void DialogTransferFunction::on_pushButton_Curve_clicked()
    {
        m_dataChosenListDialog = new DataChosenListDialog();
        m_dataChosenListDialog->setFixedSize(300, 280);
        // m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialog->setData(m_curveNameList);
        m_dataChosenListDialog->setMultipleSelected(false);
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->showWindow();
        if (m_transferFunctionType == 0)
            m_dataChosenListDialog->setSelctedIndexs(m_CurrentSelectedIndexs); // 默认选中第一个
        else if (m_transferFunctionType == 1)
            m_dataChosenListDialog->setSelctedIndexs(m_FieldStrengthSelectedIndexs); // 默认选中第一个

        connect(m_dataChosenListDialog,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &DialogTransferFunction::slot_sentChosenDataIds);
    }
    void DialogTransferFunction::on_pushButton_CalibrationCurve_clicked()
    {
        m_dataChosenListDialogCalibration = new DataChosenListDialog();
        m_dataChosenListDialogCalibration->setFixedSize(300, 280);
        // m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialogCalibration->setData(m_curveNameList);
        m_dataChosenListDialogCalibration->setMultipleSelected(false);
        m_dataChosenListDialogCalibration->updateUI();
        m_dataChosenListDialogCalibration->showWindow();
        m_dataChosenListDialogCalibration->setSelctedIndexs(m_CalibrationSelectedIndexs); // 默认选中第一个

        connect(m_dataChosenListDialogCalibration,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &DialogTransferFunction::slot_sentCalibrationChosenDataIds);
    }

    QModelIndexList DialogTransferFunction::createSelectedIndexes(const QStringList& params) {
        QModelIndexList selectedIndexes;

        foreach(const QString & param, params) {
            int row = m_curveNameList.indexOf(param);
            if (row != -1) {
                // 使用成员模型m_model创建索引，确保索引与模型生命周期一致
                QModelIndex index = m_model.index(row, 0);
                if (index.isValid()) {
                    selectedIndexes.append(index);
                }
            }
        }
        return selectedIndexes;
    }

} // namespace pst
